home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 613 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. Path: eua.ericsson.se!usenet
  2. From: euahjn@eua.ericsson.se (Henrik Johansson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Are Pure Functions always Virtual????
  5. Date: 5 Jan 1996 11:18:43 GMT
  6. Organization: Ericsson Telecom Systems Labs, Stockholm, Sweden
  7. Message-ID: <4cj1ej$ifu@euas20.eua.ericsson.se>
  8. References: <4cilse$5li@news2.deltanet.com>
  9. Reply-To: euahjn@eua.ericsson.se
  10. NNTP-Posting-Host: euas31i2c37.eua.ericsson.se
  11.  
  12. In article <4cilse$5li@news2.deltanet.com>, olivas@deltanet.com (Sergio Olivas) writes:
  13. > I have a base class for accessing databases (BC++), the base class is
  14. > made of only pure virtual functions.  Two derived classes are written,
  15. > one for accessing databases through the Paradox Engine, the other
  16. > using the Borland Database Engine.
  17. > Since I've heard that using virtual functions really eats into the 64k
  18. > automatic data segment, as well as adding overhead, is the 'virtual'
  19. > keyword really needed. -- and does it make any difference, assuming
  20. > I'm not going to further derive another class from the newly derived
  21. > class (in this case DB_BASE_PDX) ???
  22. > eg..
  23. >   class DB_BASE {
  24. > {  ...
  25. >    virtual int NextRecord(int TableID) = 0;
  26. >   ..^^^^^ is this needed?
  27. >  };
  28. > The derived classes are something like
  29. >   class DB_BASE_PDX : public DB_BASE
  30. >   {
  31. >     ...
  32. >      int NextRecord(int TableID);
  33. >    ...
  34. >    }
  35.  
  36. If you only have 2 known subclasses, and doesn't want to use virtual fns,
  37. then the alternative is to invoke the formerly virtual, now statically bound
  38. fn by using RunTime Type Identification (RTTI) combined with if-else construction
  39. and downcasting with (DB_BASE_PDX)*. This allows the compiler to inline
  40. the function and reduces overhead and memory access. Hide the invocation
  41. in a preprocessor macro, like
  42. #define NEXTRECORD(THIS,TableID) \
  43. if (...) ((DB_BASE_PDX*)THIS)->NextRecord(TableID); \
  44. else ...
  45.  
  46.